home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / builtins / read.def < prev    next >
Encoding:
Text File  |  1994-08-05  |  6.3 KB  |  253 lines

  1. This file is read.def, from which is created read.c.
  2. It implements the builtin "read" in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES read.c
  23.  
  24. $BUILTIN read
  25. $FUNCTION read_builtin
  26. $SHORT_DOC read [-r] [name ...]
  27. One line is read from the standard input, and the first word is
  28. assigned to the first NAME, the second word to the second NAME, etc.
  29. with leftover words assigned to the last NAME.  Only the characters
  30. found in $IFS are recognized as word delimiters.  The return code is
  31. zero, unless end-of-file is encountered.  If the -r option is given,
  32. this signifies `raw' input, and backslash processing is disabled.
  33. $END
  34.  
  35. #include <stdio.h>
  36. #include "../shell.h"
  37. #include "common.h"
  38.  
  39. static int stream_close ();
  40.  
  41. extern int interrupt_immediately;
  42.  
  43. /* Read the value of the shell variables whose names follow.
  44.    The reading is done from the current input stream, whatever
  45.    that may be.  Successive words of the input line are assigned
  46.    to the variables mentioned in LIST.  The last variable in LIST
  47.    gets the remainder of the words on the line.  If no variables
  48.    are mentioned in LIST, then the default variable is $REPLY.
  49.  
  50.    S. R. Bourne's shell complains if you don't name a variable
  51.    to receive the stuff that is read.  GNU's shell doesn't.  This
  52.    allows you to let the user type random things. */
  53. read_builtin (list)
  54.      WORD_LIST *list;
  55. {
  56.   register char *varname;
  57.   int size, c, i, fildes, raw_mode, pass_next, saw_escape, retval;
  58.   char *input_string, *orig_input_string, *ifs_chars;
  59.   FILE *input_stream;
  60.  
  61.   i = 0;        /* Index into the string that we are reading. */
  62.   raw_mode = 0;        /* Not reading raw input be default. */
  63.  
  64.   while (list)
  65.     {
  66.       if (ISOPTION (list->word->word, 'r'))
  67.     {
  68.       raw_mode = 1;
  69.       list = list->next;
  70.     }
  71.       else if (ISOPTION (list->word->word, '-'))
  72.     {
  73.       list = list->next;
  74.       break;
  75.     }
  76.       else if (*list->word->word == '-')
  77.     {
  78.       bad_option (list->word->word);
  79.       builtin_error ("usage: read [-r] [name ...]");
  80.       return (EX_USAGE);
  81.     }
  82.       else
  83.     break;
  84.     }
  85.  
  86.   /* We need unbuffered input from stdin.  So we make a new stream with
  87.      the same file descriptor as stdin, then unbuffer it. */
  88.   fildes = dup (fileno (stdin));
  89.  
  90.   if (fildes == -1)
  91.     return (EXECUTION_FAILURE);
  92.  
  93.   input_stream = fdopen (fildes, "r");
  94.  
  95.   if (!input_stream)
  96.     {
  97.       close (fildes);
  98.       return (EXECUTION_FAILURE);
  99.     }
  100.  
  101.   ifs_chars = get_string_value ("IFS");
  102.   input_string = xmalloc (size = 128);
  103.  
  104.   setbuf (input_stream, (char *)NULL);
  105.  
  106.   begin_unwind_frame ("read_builtin");
  107.   add_unwind_protect (xfree, input_string);
  108.   add_unwind_protect (stream_close, input_stream);
  109.   interrupt_immediately++;
  110.  
  111.   pass_next = 0;    /* Non-zero signifies last char was backslash. */
  112.   saw_escape = 0;    /* Non-zero signifies that we saw an escape char */
  113.  
  114.   while ((c = getc (input_stream)) != EOF)
  115.     {
  116.       if (i + 2 >= size)
  117.     input_string = xrealloc (input_string, size += 128);
  118.  
  119.       /* If the next character is to be accepted verbatim, a backslash
  120.      newline pair still disappears from the input. */
  121.       if (pass_next)
  122.     {
  123.       if (c == '\n')
  124.         i--;        /* back up over the CTLESC */
  125.       else
  126.         input_string[i++] = c;
  127.       pass_next = 0;
  128.       continue;
  129.     }
  130.  
  131.       if (c == '\\' && !raw_mode)
  132.     {
  133.       pass_next++;
  134.       saw_escape++;
  135.       input_string[i++] = CTLESC;
  136.       continue;
  137.     }
  138.  
  139.       if (c == '\n')
  140.     break;
  141.  
  142.       if (c == CTLESC || c == CTLNUL)
  143.     input_string[i++] = CTLESC;
  144.  
  145.       input_string[i++] = c;
  146.     }
  147.   input_string[i] = '\0';
  148.  
  149.   interrupt_immediately--;
  150.   discard_unwind_frame ("read_builtin");
  151.  
  152.   fclose (input_stream);
  153.  
  154.   if (c == EOF)
  155.     {
  156.       retval = EXECUTION_FAILURE;
  157.       input_string[0] = '\0';
  158.     }
  159.   else
  160.     retval = EXECUTION_SUCCESS;
  161.  
  162.   if (!list)
  163.     {
  164.       SHELL_VAR *var;
  165.       char *t;
  166.  
  167.       if (saw_escape)
  168.     {
  169.       t = dequote_string (input_string);
  170.       var = bind_variable ("REPLY", t);
  171.       free (t);
  172.     }
  173.       else
  174.     var = bind_variable ("REPLY", input_string);
  175.       var->attributes &= ~att_invisible;
  176.       free (input_string);
  177.     }
  178.   else
  179.     {
  180.       SHELL_VAR *var;
  181.       char *t;
  182.       /* This code implements the Posix.2 spec for splitting the words
  183.      read and assigning them to variables.  If $IFS is unset, we
  184.      use the default value of " \t\n". */
  185.       if (!ifs_chars)
  186.     ifs_chars = "";
  187.  
  188.       orig_input_string = input_string;
  189.       while (list->next)
  190.     {
  191.       char *e, *t1;
  192.  
  193.       varname = list->word->word;
  194.  
  195.       /* If there are more variables than words read from the input,
  196.          the remaining variables are set to the empty string. */
  197.       if (*input_string)
  198.         {
  199.           /* This call updates INPUT_STRING. */
  200.           t = get_word_from_string (&input_string, ifs_chars, &e);
  201.           if (t)
  202.         *e = '\0';
  203.           /* Don't bother to remove the CTLESC unless we added one
  204.          somewhere while reading the string. */
  205.           if (t && saw_escape)
  206.         {
  207.           t1 = dequote_string (t);
  208.           var = bind_variable (varname, t1);
  209.           free (t1);
  210.         }
  211.           else
  212.         var = bind_variable (varname, t);
  213.         }
  214.       else
  215.         {
  216.           t = (char *)0;
  217.           var = bind_variable (varname, "");
  218.         }
  219.  
  220.       stupidly_hack_special_variables (varname);
  221.       var->attributes &= ~att_invisible;
  222.  
  223.       if (t)
  224.         free (t);
  225.  
  226.       list = list->next;
  227.     }
  228.  
  229.       if (saw_escape)
  230.     {
  231.       t = dequote_string (input_string);
  232.       var = bind_variable (list->word->word, t);
  233.       free (t);
  234.     }
  235.       else
  236.     var = bind_variable (list->word->word, input_string);
  237.       stupidly_hack_special_variables (list->word->word);
  238.       var->attributes &= ~att_invisible;
  239.       free (orig_input_string);
  240.     }
  241.  
  242.   return (retval);
  243. }
  244.  
  245. /* This way I don't have to know whether fclose () is a
  246.    function or a macro. */
  247. static int
  248. stream_close (file)
  249.      FILE *file;
  250. {
  251.   return (fclose (file));
  252. }
  253.